/*===============================================
 *   文件名称：port.c
 *   创 建 者: 麦穗飘飘
 *   创建日期：2024年12月12日   17:35:56
 *   描    述：串口通信模块
 ================================================*/
#include "port.h"

char arr[4][1024];  // 定义一个 4x4 的字符数组
struct get_information datas;
float Hcsr04GetLength;

/* 定义一个函数，将字符串转换为浮点型 */
float string_to_float(const char *buf) {
    float sum = 0.0;     // 定义一个浮点型变量
    char *token = strtok((char *)buf, " ");  // 以空格分隔字符串

    while (token != NULL) {
        float value = atof(token);
        sum += value;
        token = strtok(NULL, " ");
    }
    return sum;
}

/* 服务器端接收客户端请求 */
int sever_to_client(int port, int *fd){ 
    *fd = socket(AF_INET, SOCK_STREAM, 0);  // 创建 socket
    if(*fd < 0){
        perror("socket");
        return 0;
    }

    int opt = 1;
    // 设置 socket 选项
    if (setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {      
        perror("setsockopt");
        close(*fd);
        return -1;
    }

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr("192.168.2.56");
    //绑定
    if(bind(*fd, (struct sockaddr *)&addr, sizeof(addr))){
        perror("bind");
    }
    //设置监听，模式
    if(listen(*fd, 3)){
        perror("listen");
    }
    //接受请求
    struct sockaddr_in cli_addr;
    socklen_t len = sizeof(cli_addr);
    int fbap = accept(*fd, (struct sockaddr *)(&cli_addr), &len);
    if(fbap < 0){
        perror("accept");
    }
    // printf("accept ok\n");
    printf("Qt connect A9_Board accept ok\n");
    return fbap;
}

/* 读取串口数据 */
int get_bufs() {    
    int sfd = serial_open("/dev/ttyUSB0");    // 打开串口对应 A9Board 的设备ttyUSB0
    if (sfd == -1) {
        perror("open");
        return -1;
    }
    printf("open device\n");
    if (serial_config(sfd) != 0) {
        perror("serial configure");
        serial_close(sfd);
        return -1;
    }

    char buf[1024]; // 定义buf接受STM32发送的数据

    int i;
    while (1) {
        for(i = 0; i < 4; i++) {
            memset(buf, 0, sizeof(buf));
            int ret = serial_read_image(sfd, buf);    // 读取串口数据
            if (ret == -1) {
                perror("read");
                printf("Error number: %d\n", errno);
                printf("Error string: %s\n", strerror(errno));
                printf("here\n");
                return -1;
            }
            // printf("arr[%d] = %s", i, buf);
            // 复制 buf 内容到 arr[i] 中
            if (buf[0] == '0') {    // 遇到 0 则跳过
                if(string_to_float(buf + 1) == 0){
                    continue;
                }
                printf("0\n");
                strncpy(datas.temperature, buf + 1, sizeof(datas.temperature) - 1); 
                datas.temperature[sizeof(datas.temperature) - 1] = '\0';
                printf("datas.temperature = %s", datas.temperature);
            }
            if (buf[0] == '1') {
                printf("1\n");
                strncpy(datas.humidity, buf + 1, sizeof(datas.humidity) - 1);
                datas.humidity[sizeof(datas.humidity) - 1] = '\0';
                printf("datas.humidity = %s", datas.humidity);
            }
            if (buf[0] == '2') {
                printf("2\n");
                Hcsr04GetLength = string_to_float(buf + 1);
                printf("Hcsr04GetLength = %f\n", Hcsr04GetLength);
            }
            if (buf[0] == '3') {
                printf("3\n");
                strncpy(datas.MQ_2, buf + 1, sizeof(datas.MQ_2) - 1);
                datas.MQ_2[sizeof(datas.MQ_2) - 1] = '\0';
                printf("datas.MQ_2 = %s", datas.MQ_2);
            }
        }
    }
}

/*发送信号到 STM32 设备 */
int send_signal(char *data){ 
    int sfd = serial_open("/dev/ttyUSB0");
    if (sfd == -1) {
        perror("open");
        return -1;
    }
    printf("open STM32 device\n");

    if (serial_config(sfd) != 0) {
        perror("serial configure");
        serial_close(sfd);
        return -1;
    }
    char buf[1024];
    serial_write(sfd,data);
}

